const fs = require('fs')

const rs = fs.createReadStream('./data/sample.txt', 'utf-8')

rs.on('data', chunk => {
  console.log('DATA: ')
  console.log(chunk)
})

rs.on('end', () => {
  console.log('END')
})

rs.on('error', err => {
  console.log(`ERROR: ${err}`)
})

const ws1 = fs.createWriteStream('./data/output1.txt', 'utf-8')
ws1.write('使用Stream写入文本数据...\n')
ws1.write('END.')
ws1.end()

const ws2 = fs.createWriteStream('./data/output2.txt', 'utf-8')
ws2.write(new Buffer('使用二进制写入文本数据...\n', 'utf-8'))
ws2.write(new Buffer('END.', 'utf-8'))
ws2.end()

const rs3 = fs.createReadStream('./data/sample.txt')
const ws3 = fs.createWriteStream('./data/copy.txt')

rs3.pipe(ws3)